//
//  StewareDetailViewController.m
//  VTravel
//
//  Created by lanouhn on 16/8/4.
//  Copyright © 2016年 xiaoYu. All rights reserved.
//

#import "StewareDetailViewController.h"

@interface StewareDetailViewController ()<UITableViewDelegate, UITableViewDataSource>

/** 表头视图 */
@property (nonatomic, strong) UIView *tableHeaderView;
@property (nonatomic, strong) UIView *headerIV;
/** 底部的表视图 */
@property (strong, nonatomic) IBOutlet UITableView *rootTableView;

/** cell标题数据源数组 */
@property (nonatomic, strong) NSArray *dataArray;

@property(nonatomic,strong)NSMutableDictionary *stewardDic;

@end

@implementation StewareDetailViewController

#pragma mark - 网络请求数据的方法
- (void)requestData{
    [NetWorkManager requestForGETWithURL:StrByAppendStr(KSteward_Detail_URL, self.stewareId) paramater:nil success:^(id responseObject) {
        [self hideHUD];
        self.stewardDic = responseObject[@"data"];
        
        [self creatTableHeaderView];
        self.rootTableView.tableHeaderView = self.tableHeaderView;

        dispatch_async(dispatch_get_main_queue(), ^{
            [self.rootTableView reloadData];
        });
        
    } fail:^(NSError *error) {
        
    }];
}

#pragma mark - 配置表头视图
- (void)creatTableHeaderView{
    self.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 200)];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 200)];
    imageView.image = [UIImage imageNamed:@"个人背景"];
    UIImageView *headerIV = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth / 2 - 45, 30, 90, 90)];
    headerIV.layer.cornerRadius = 45;
    headerIV.layer.masksToBounds = YES;
//    headerIV.backgroundColor = [UIColor cyanColor];
    if (self.stewardDic[@"avatar"] != nil) {
        [headerIV sd_setImageWithURL:[NSURL URLWithString:ImageURL(StrByAppendStr(@"/",self.stewardDic[@"avatar"]))]];
    }
    [imageView addSubview:headerIV];
    [self.tableHeaderView addSubview:imageView];
    
    UILabel *mottoLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 150, kScreenWidth, 30)];
    mottoLabel.font = [UIFont systemFontOfSize:12];
    if (![self.stewardDic[@"motto"] isKindOfClass:[NSNull class]]) {
        mottoLabel.text = [NSString stringWithFormat:@"  %@",self.stewardDic[@"motto"]];
    }
    [self.tableHeaderView addSubview:mottoLabel];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.stewardDic = [NSMutableDictionary dictionary];
    self.dataArray = @[@"管家部落", @"精通业务", @"管家所属", @"公司地址", @"联系电话", @"个人邮箱"];
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        // 请求网络数据
        [self requestData];
    });
    // 配置表头视图
    [self creatTableHeaderView];
    self.rootTableView.tableHeaderView = self.tableHeaderView;
    
    self.rootTableView.scrollEnabled = NO;
    
}

#pragma mark - 返回分区数的方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
#pragma mark -返回分区行数的方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 6;
    }else{
        return 1;
    }
}
#pragma mark - 返回cell的方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    // 重用
    static NSString *identifier = @"CELL";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell  == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:identifier];
    }
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        // 给每个cell添加一个imageView
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 20, 20)];
        imageView.image = [UIImage imageNamed:self.dataArray[indexPath.row]];
        [cell addSubview:imageView];
        // 设置cell的 textLabel
        cell.textLabel.text = self.dataArray[indexPath.row];
        cell.textLabel.textColor = [UIColor blackColor];
        // 设置每个cell的 detailTextLabel
        NSArray *sellerArray = self.stewardDic[@"sellers"];
        NSDictionary *dic = [sellerArray firstObject];
        cell.detailTextLabel.textColor = [UIColor grayColor];
        switch (indexPath.row) {
            case 0:
                cell.detailTextLabel.text = nil;
                break;
            case 1:
                if (![self.stewardDic[@"advantage"] isKindOfClass:[NSNull class]]) {
                    cell.detailTextLabel.text = self.stewardDic[@"advantage"];
                    NSLog(@"2");
                }
                break;
            case 2:
                if (![dic[@"name"] isKindOfClass:[NSNull class]]) {
                    cell.detailTextLabel.text = dic[@"name"];
                }
                break;
            case 3:
                if (![dic[@"address"] isKindOfClass:[NSNull class]]) {
                    cell.detailTextLabel.text = dic[@"address"];
                }
                break;
            case 4:
                if (![self.stewardDic[@"mobile"] isKindOfClass:[NSNull class]]) {
                    cell.detailTextLabel.text = self.stewardDic[@"mobile"];
                }
                break;
            case 5:
                if (![self.stewardDic[@"email"] isKindOfClass:[NSNull class]]) {
                    cell.detailTextLabel.text = self.stewardDic[@"email"];
                }
                break;
            default:
                break;
        }
    }else{
        // 配置第二个分区的两个button
        UIButton *LeftButton = [UIButton buttonWithType:UIButtonTypeCustom];
        LeftButton.frame = CGRectMake(10, 2, (kScreenWidth - 30) / 2, 35);
        [LeftButton setImage:[UIImage imageNamed:@"绑定"] forState:(UIControlStateNormal)];
        [LeftButton addTarget:self action:@selector(handleLeftButtonAction) forControlEvents:(UIControlEventTouchUpInside)];
        [cell addSubview:LeftButton];
        UILabel *LeftLable = [[UILabel alloc] initWithFrame:CGRectMake((kScreenWidth - 30) / 4 - 20, 5, 40, 20)];
        LeftLable.text = @"绑定";
        LeftLable.font = [UIFont systemFontOfSize:15];
        LeftLable.textColor = [UIColor whiteColor];
        [LeftButton addSubview:LeftLable];
        
        UIButton *RightButton = [UIButton buttonWithType:UIButtonTypeCustom];
        RightButton.frame = CGRectMake((kScreenWidth - 30) / 2 + 20, 2, (kScreenWidth - 30) / 2, 35);
        [RightButton setImage:[UIImage imageNamed:@"咨询"] forState:(UIControlStateNormal)];
        [RightButton addTarget:self action:@selector(handleRightButtonAction) forControlEvents:(UIControlEventTouchUpInside)];
        
        UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake((kScreenWidth - 30) / 4 - 20, 5, 40, 20)];
        lable.text = @"咨询";
        lable.font = [UIFont systemFontOfSize:15];
        lable.textColor = [UIColor whiteColor];
        [RightButton addSubview:lable];
        
        [cell addSubview:RightButton];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
#pragma mark - 点击绑定按钮触发的事件
- (void)handleLeftButtonAction{
    kMyLog(@"点击了绑定按钮");
}
#pragma mark - 点击咨询按钮触发的事件
- (void)handleRightButtonAction{
    kMyLog(@"点击了咨询按钮");
    
    // 创建提示框
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"要打电话该他(她)吗" preferredStyle:(UIAlertControllerStyleAlert)];
    
    UIAlertAction *rejectAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        kMyLog(@"确定打电话给该管家");
    }];
    
    UIAlertAction *acceptAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    // 将事件给控制器
    [alertController addAction:rejectAction];
    [alertController addAction:acceptAction];
    // 弹出提示框
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
}
#pragma mark - 返回cell的高度的方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 44;
    }else{
        return 100;
    }
}
#pragma mark - 返回区头
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}
#pragma mark - cell的点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0 && indexPath.row == 0) {
        if (indexPath.row == 0) {
            kMyLog(@"点击了管家部落");
        }
    }
}

@end
